/*
 * DialogParte
 *
 * File created on 06-dic-2009
 * Copyright (c) 2009 Lorenzo Carbonell
 * email: lorenzo.carbonell.cerezo@gmail.com
 * website: http://www.atareao.es
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package es.atareao.ferraplan.gui;
//
//********************************IMPORTACIONES*********************************
//
import es.atareao.alejandria.gui.ErrorDialog;
import es.atareao.alejandria.lib.Convert;
import es.atareao.alejandria.lib.DateUtils;
import es.atareao.ferraplan.lib.Elaborador;
import es.atareao.ferraplan.lib.Fabricante;
import es.atareao.ferraplan.lib.Montador;
import es.atareao.ferraplan.lib.Revision;
import java.awt.event.ItemEvent;
import java.sql.SQLException;
import javax.swing.JFrame;


/**
 *
 * @author  Lorenzo Carbonell
 */
public class DialogRevision extends javax.swing.JDialog {
    //
    //********************************CONSTANTES********************************
    //
    /** A return status code - returned if Cancel button has been pressed */
    public static final int RET_CANCEL = 0;
    /** A return status code - returned if OK button has been pressed */
    public static final int RET_OK = 1;
    //
    public final static int OP_ADD=0;
    public final static int OP_EDIT=1;
    public final static int OP_DELETE=2;
    public final static int OP_VIEW=3;
    //
    //******************************CONSTRUCTORES*******************************
    //
    /**
     * Creates new form DialogParte
     * @param padre 
     * @param operacion 
     * @param empresa 
     * @throws java.sql.SQLException 
     */
    public DialogRevision(JFrame padre,int operacion,Revision revision) throws SQLException {
        super(padre,true);
        initComponents();
        this.setSize(420,300);
        this.setLocationRelativeTo(null);
        this.setOperacion(operacion);
        //
        switch (operacion){
            case OP_ADD:
                this.setTitle("Añadir Revisión");
                break;
            case OP_EDIT:
                this.setTitle("Editar Revisión");
                break;
            case OP_DELETE:
                this.setTitle("Borrar Revisión");
                this.doNotEditable();
                break;
            case OP_VIEW:
                this.setTitle("Consultar Revisión");
                this.doNotEditable();
                break;
        }
        //
        Fabricante fabricante=new Fabricante(revision.getConector());
        this.jFabricante.setElements(fabricante.findAll());
        Elaborador elaborador=new Elaborador(revision.getConector());
        this.jElaborador.setElements(elaborador.findAll());
        Montador montador=new Montador(revision.getConector());
        this.jMontador.setElements(montador.findAll());
        this.setRevision(revision);
    }
    
    public DialogRevision(int operacion,Revision revision) throws SQLException{
        this(new JFrame(),operacion,revision);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jAceptar = new javax.swing.JButton();
        jCancelar = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jRevision = new es.atareao.alejandria.gui.JNumericField();
        jFechaRevision = new es.atareao.alejandria.gui.JDateChooser();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jPedido = new javax.swing.JCheckBox();
        jFechaPedido = new es.atareao.alejandria.gui.JDateChooser();
        jElaborador = new es.atareao.queensboro.gui.JWrapperComboBox();
        jPanel3 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jSuministrado = new javax.swing.JCheckBox();
        jFechaSuministrado = new es.atareao.alejandria.gui.JDateChooser();
        jFabricante = new es.atareao.queensboro.gui.JWrapperComboBox();
        jPanel4 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jMontado = new javax.swing.JCheckBox();
        jFechaMontado = new es.atareao.alejandria.gui.JDateChooser();
        jMontador = new es.atareao.queensboro.gui.JWrapperComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/es/atareao/img/png/button_ok.png"))); // NOI18N
        jAceptar.setBorderPainted(false);
        jAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jAceptarActionPerformed(evt);
            }
        });
        getContentPane().add(jAceptar, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 210, 50, 50));

        jCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/es/atareao/img/png/button_cancel.png"))); // NOI18N
        jCancelar.setBorderPainted(false);
        jCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCancelarActionPerformed(evt);
            }
        });
        getContentPane().add(jCancelar, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 210, 50, 50));

        jTabbedPane1.setBorder(null);

        jPanel1.setBorder(null);
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("Revisión:");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 100, 30));

        jLabel2.setText("Fecha:");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, 100, 30));
        jPanel1.add(jRevision, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 20, 260, 30));
        jPanel1.add(jFechaRevision, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 60, 260, 30));

        jTabbedPane1.addTab("Revision", jPanel1);

        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel3.setText("Pedido:");
        jPanel2.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 330, 30));

        jLabel4.setText("Fecha:");
        jPanel2.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, 160, 30));

        jLabel8.setText("Elaborador:");
        jPanel2.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 100, 160, 30));

        jPedido.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jPedidoItemStateChanged(evt);
            }
        });
        jPanel2.add(jPedido, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 20, 20, 30));

        jFechaPedido.setEnabled(false);
        jPanel2.add(jFechaPedido, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 60, -1, 30));

        jElaborador.setEnabled(false);
        jPanel2.add(jElaborador, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 100, 200, 30));

        jTabbedPane1.addTab("Pedido", jPanel2);

        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel5.setText("Suministrado:");
        jPanel3.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 330, 30));

        jLabel6.setText("Fabricante:");
        jPanel3.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 100, 160, 30));

        jLabel7.setText("Fecha:");
        jPanel3.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, 160, 30));

        jSuministrado.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jSuministradoItemStateChanged(evt);
            }
        });
        jPanel3.add(jSuministrado, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 20, 20, 30));

        jFechaSuministrado.setEnabled(false);
        jPanel3.add(jFechaSuministrado, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 60, -1, 30));

        jFabricante.setEnabled(false);
        jPanel3.add(jFabricante, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 100, 200, 30));

        jTabbedPane1.addTab("Suministrado", jPanel3);

        jPanel4.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel9.setText("Montado:");
        jPanel4.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, 330, 30));

        jLabel10.setText("Montador:");
        jPanel4.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 100, 160, 30));

        jLabel11.setText("Fecha:");
        jPanel4.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, 160, 30));

        jMontado.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jMontadoItemStateChanged(evt);
            }
        });
        jPanel4.add(jMontado, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 20, 20, 30));

        jFechaMontado.setEnabled(false);
        jPanel4.add(jFechaMontado, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 60, -1, 30));

        jMontador.setEnabled(false);
        jPanel4.add(jMontador, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 100, 200, 30));

        jTabbedPane1.addTab("Montado", jPanel4);

        getContentPane().add(jTabbedPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 390, 180));

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void jCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCancelarActionPerformed
// TODO add your handling code here:
        doClose(RET_CANCEL);
    }//GEN-LAST:event_jCancelarActionPerformed
    
    private void jAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jAceptarActionPerformed
// TODO add your handling code here:
        doClose(RET_OK);
    }//GEN-LAST:event_jAceptarActionPerformed

    private void jSuministradoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jSuministradoItemStateChanged
        boolean suministrado=false;
        if(evt.getStateChange()==ItemEvent.SELECTED){
            suministrado=true;
            this.jFechaSuministrado.setDate(DateUtils.Ahora());
            this.jFabricante.setSelectedIndex(0);
        }
        this.jFechaSuministrado.setEnabled(suministrado);
        this.jFabricante.setEnabled(suministrado);
    }//GEN-LAST:event_jSuministradoItemStateChanged

    private void jMontadoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jMontadoItemStateChanged
        boolean montado=false;
        if(evt.getStateChange()==ItemEvent.SELECTED){
            montado=true;
            this.jFechaMontado.setDate(DateUtils.Ahora());
            this.jMontador.setSelectedIndex(0);
        }
        this.jFechaMontado.setEnabled(montado);
        this.jMontador.setEnabled(montado);
    }//GEN-LAST:event_jMontadoItemStateChanged

    private void jPedidoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jPedidoItemStateChanged
        boolean pedido=false;
        if(evt.getStateChange()==ItemEvent.SELECTED){
            pedido=true;
            this.jFechaPedido.setDate(DateUtils.Ahora());
            this.jElaborador.setSelectedIndex(0);
        }
        this.jFechaPedido.setEnabled(pedido);
        this.jElaborador.setEnabled(pedido);
    }//GEN-LAST:event_jPedidoItemStateChanged
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jAceptar;
    private javax.swing.JButton jCancelar;
    private es.atareao.queensboro.gui.JWrapperComboBox jElaborador;
    private es.atareao.queensboro.gui.JWrapperComboBox jFabricante;
    private es.atareao.alejandria.gui.JDateChooser jFechaMontado;
    private es.atareao.alejandria.gui.JDateChooser jFechaPedido;
    private es.atareao.alejandria.gui.JDateChooser jFechaRevision;
    private es.atareao.alejandria.gui.JDateChooser jFechaSuministrado;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JCheckBox jMontado;
    private es.atareao.queensboro.gui.JWrapperComboBox jMontador;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JCheckBox jPedido;
    private es.atareao.alejandria.gui.JNumericField jRevision;
    private javax.swing.JCheckBox jSuministrado;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables
    //
    // *********************************CAMPOS*********************************
    //
    private int _returnStatus = RET_CANCEL;
    private int _operacion;
    private Revision _revision;
    //
    //********************************METODOS***********************************
    //
    
    //
    //**************************METODOS AUXILIARES******************************
    //
    private void doClose(int retStatus) {
        if(retStatus==RET_OK){
            try {
                if(this.fromDialog().validate()){
                    this.setRevision(this.fromDialog());
                    this.setReturnStatus(retStatus);
                    setVisible(false);
                    dispose();
                }
            } catch (Exception ex) {
                 ErrorDialog.manejaError(ex,false);
            }
        }else{
            this.setReturnStatus(retStatus);
            setVisible(false);
            dispose();
        }
    }    

    private void doNotEditable(){
        this.jRevision.setEditable(false);
        this.jFechaRevision.setEditable(false);
        //
        this.jPedido.setEnabled(false);
        this.jFechaPedido.setEditable(false);
        this.jElaborador.setEditable(false);
        //
        this.jSuministrado.setEnabled(false);
        this.jFechaSuministrado.setEditable(false);
        this.jFabricante.setEditable(false);
        //
        this.jMontado.setEnabled(false);
        this.jFechaMontado.setEditable(false);
        this.jMontador.setEditable(false);
        //
        this.jRevision.setBackground(java.awt.SystemColor.info);
        this.jFechaRevision.setBackground(java.awt.SystemColor.info);
        this.jPedido.setBackground(java.awt.SystemColor.info);
        this.jFechaPedido.setBackground(java.awt.SystemColor.info);
        this.jElaborador.setBackground(java.awt.SystemColor.info);
        this.jSuministrado.setBackground(java.awt.SystemColor.info);
        this.jFechaSuministrado.setBackground(java.awt.SystemColor.info);
        this.jFabricante.setBackground(java.awt.SystemColor.info);
        this.jMontado.setBackground(java.awt.SystemColor.info);
        this.jFechaMontado.setBackground(java.awt.SystemColor.info);
        this.jMontador.setBackground(java.awt.SystemColor.info);
    }
    
    private Revision fromDialog(){
        String revision=jRevision.getText();
        String fecha=Convert.toStringDateSql(jFechaRevision.getDate());
        String pedida_esta=Convert.toString(this.jPedido.isSelected());
        String pedida_fecha=Convert.toStringDateSql(this.jFechaPedido.getDate());
        String suministrada_esta=Convert.toString(this.jSuministrado.isSelected());
        String suministrada_fecha=Convert.toStringDateSql(this.jFechaSuministrado.getDate());
        String montada_esta=Convert.toString(this.jMontado.isSelected());
        String montada_fecha=Convert.toStringDateSql(this.jFechaMontado.getDate());
        String fabricante_id=this.jFabricante.getSelectedId();
        String montador_id=this.jMontador.getSelectedId();
        String elaborador_id=this.jElaborador.getSelectedId();
        this.getRevision().setValue("revision", revision);
        this.getRevision().setValue("fecha", fecha);
        this.getRevision().setValue("pedida_esta", pedida_esta);
        this.getRevision().setValue("pedida_fecha", pedida_fecha);
        this.getRevision().setValue("suministrada_esta", suministrada_esta);
        this.getRevision().setValue("suministrada_fecha", suministrada_fecha);
        this.getRevision().setValue("montada_esta", montada_esta);
        this.getRevision().setValue("montada_fecha", montada_fecha);
        this.getRevision().setValue("fabricante_id", fabricante_id);
        this.getRevision().setValue("montador_id", montador_id);
        this.getRevision().setValue("elaborador_id", elaborador_id);
        return this.getRevision();
    }
    private void toDialog(Revision revision){
        String nrevision=revision.getValue("revision");
        String fecha=revision.getValue("fecha");
        String pedida_esta=revision.getValue("pedida_esta");
        String pedida_fecha=revision.getValue("pedida_fecha");
        String suministrada_esta=revision.getValue("suministrada_esta");
        String suministrada_fecha=revision.getValue("suministrada_fecha");
        String montada_esta=revision.getValue("montada_esta");
        String montada_fecha=revision.getValue("montada_fecha");
        String fabricante_id=revision.getValue("fabricante_id");
        String montador_id=revision.getValue("montador_id");
        String elaborador_id=revision.getValue("elaborador_id");
        this.jRevision.setDouble(nrevision);
        this.jFechaRevision.setDate(Convert.toDateSql(fecha));
        this.jPedido.setSelected(Convert.toBoolean(pedida_esta));
        this.jFechaPedido.setDate(Convert.toDateSql(pedida_fecha));
        this.jSuministrado.setSelected(Convert.toBoolean(suministrada_esta));
        this.jFechaSuministrado.setDate(Convert.toDateSql(suministrada_fecha));
        this.jMontado.setSelected(Convert.toBoolean(montada_esta));
        this.jFechaMontado.setDate(Convert.toDateSql(montada_fecha));
        this.jFabricante.setSelectedId(fabricante_id);
        this.jElaborador.setSelectedId(elaborador_id);
        this.jMontador.setSelectedId(montador_id);
    } 
    
    //
    //**************************METODOS DE ACCESO*******************************
    //
    
    public int getReturnStatus() {
        return _returnStatus;
    }
    
    public void setReturnStatus(int returnStatus) {
        this._returnStatus = returnStatus;
    }
    
    public int getOperacion() {
        return _operacion;
    }

    public void setOperacion(int operacion) {
        this._operacion = operacion;
    }

    /**
     * @return the _revision
     */
    public Revision getRevision() {
        return _revision;
    }

    /**
     * @param revision the _revision to set
     */
    public void setRevision(Revision revision) {
        this._revision = revision;
        this.toDialog(revision);
    }
    
}
